//  connect  NodeJS 链接MongoDB 数据 

//  Mongoose是在node.js环境下对mongodb进行便捷操作的对象模型工具




var mongoose = require("mongoose")

const hostname = "localhost"
const port = 27017
const dbname = "ly"
const user = "ly9624"
const pass = "962464"

const db_conn_str = `mongodb://39.100.106.156:27017/kaola`
// const db_conn_str = `mongodb://${hostname}:${port}/${dbname}`


mongoose.connect(db_conn_str, {
    useNewUrlParser: true,
    useUnifiedTopology: true
}, function (err) {
    if (err) {
        console.log(err)
        console.log("数据库链接失败")
    } else {
        console.log("数据库链接成功 in")

    }
})

const connection = mongoose.connection

connection.on("connected", function () {
    console.log("数据库已经正确链接---connected")
})

connection.on("error", function (err) {
    console.log("数据库无法正常链接---error")
})

connection.on("disconnected", () => {
    console.log("数据库已经断开链接---disconnected")
})

module.exports = connection;